/**
 *  Created on  : 16/05/2012
 *  Author      : Ing. Javier Silva Pérez - [javier]
 *  Description :
 *  	
 */
package cinvestav.cryptography.pki.android.utils;

import java.security.SecureRandom;

import org.spongycastle.crypto.Digest;
import org.spongycastle.crypto.digests.MD2Digest;
import org.spongycastle.crypto.digests.MD4Digest;
import org.spongycastle.crypto.digests.MD5Digest;
import org.spongycastle.crypto.digests.RIPEMD128Digest;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import org.spongycastle.crypto.digests.RIPEMD256Digest;
import org.spongycastle.crypto.digests.SHA1Digest;
import org.spongycastle.crypto.digests.SHA224Digest;
import org.spongycastle.crypto.digests.SHA256Digest;
import org.spongycastle.crypto.digests.SHA384Digest;
import org.spongycastle.crypto.digests.SHA512Digest;

import cinvestav.cryptography.pki.android.exception.AndroidCryptoUtilsException;

/**
 * @author Ing. Javier Silva Pérez - [javier]
 *
 */
public class AndroidCryptoUtils {
	public static final String PROVIDER = "SC";
	
	public static final String ENCODER_BASE64 = "ENCODER_BASE64";
	public static final String ENCODER_HEX = "ENCODER_HEX";
	
	public static final String ENCODING_DER = "DER";
	public static final String ENCODING_PEM = "PEM";
	
	public static SecureRandom secureRandom = new SecureRandom(SecureRandom.getSeed(512));
	
	public static final String DIGEST_SIGN_FUNCTION_RIPEMD128 = "RIPEMD128";
	public static final String DIGEST_SIGN_FUNCTION_RIPEMD160 = "RIPEMD160";
	public static final String DIGEST_SIGN_FUNCTION_RIPEMD256 = "RIPEMD256";

	public static final String DIGEST_SIGN_FUNCTION_SHA_1 = "SHA-1";
	public static final String DIGEST_SIGN_FUNCTION_SHA_224 = "SHA-224";
	public static final String DIGEST_SIGN_FUNCTION_SHA_256 = "SHA-256";
	public static final String DIGEST_SIGN_FUNCTION_SHA_384 = "SHA-384";
	public static final String DIGEST_SIGN_FUNCTION_SHA_512 = "SHA-512";

	public static final String DIGEST_SIGN_FUNCTION_MD2 = "MD2";
	public static final String DIGEST_SIGN_FUNCTION_MD4 = "MD4";
	public static final String DIGEST_SIGN_FUNCTION_MD5 = "MD5";
	
	/**
	 * Creates an instance of a Digest depending on the name of it
	 * 
	 * @param digestName
	 *            Name of the desired Digest
	 * @return An instance of a Digest object
	 * @throws AndroidCryptoUtilsException
	 *             If the name does not correspond to a supported digest
	 *             algorithm
	 */
	public static Digest selectDigest(String digestName)
			throws AndroidCryptoUtilsException {
		// Check the digest Name and create a Digest object
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_MD2)) {
			return new MD2Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_MD4)) {
			return new MD4Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_MD5)) {
			return new MD5Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_RIPEMD128)) {
			return new RIPEMD128Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_RIPEMD160)) {
			return new RIPEMD160Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_RIPEMD256)) {
			return new RIPEMD256Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_SHA_1)) {
			return new SHA1Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_SHA_224)) {
			return new SHA224Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_SHA_256)) {
			return new SHA256Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_SHA_384)) {
			return new SHA384Digest();
		}
		if (digestName.endsWith(DIGEST_SIGN_FUNCTION_SHA_512)) {
			return new SHA512Digest();
		}

		throw new AndroidCryptoUtilsException("Selected Digest [" + digestName
				+ "] is not supported");
	}
}
